<?php

/**
 * RequiredValidator is subclass of CRequiredValidator.
 *
 * The validation will only occur if $dependOn is null or value of $dependOn
 * attribute is evaluate to true.
 *
 * Example:
 * minValue attribute is required only if hasRange attribute value is true and
 *
 * The rule:
 * array(
 *     'minValue',
 *     'RequiredValidator',
 *     'dependOnAttribute'=>'hasRange',
 * )
 *
 * @author Julot
 * @since Wednesday, February 16th, 2011
 */
class RequiredValidator extends CRequiredValidator {
	/**
	 * @property string $dependOn the attribute name that this validator depend
	 *     on.
	 */
	public $dependOnAttribute = null;
	
	/**
	 * Validate only if $dependOnAttribute is null or value of
	 * $dependOnAttribute is true.
	 * @param CModel $object
	 * @param string $attribute
	 */
	public function validateAttribute($object, $attribute) {
		if ($this->dependOnAttribute) {
			if ($object->getAttribute($this->dependOnAttribute)) {
				parent::validateAttribute($object, $attribute);
			}
		}
		else parent::validateAttribute($object, $attribute);
	}
}